class GoodDetail {
    constructor() {
        this.init();

    }
    // async是对promise的一次更改
    async init() {
        this.goods_detail = document.querySelector(".leftpart");
        this.img = document.querySelector(".img-box img");
        this.big_img = document.querySelector(".big-img img");
        this.min_img = document.querySelector(".minpic img")
        this.price = document.querySelector(".price")
        this.title = document.querySelector(".titleBox .title");
        this.id = location.hash.split("=")[1];
        this.buycar = document.querySelector(".buycar");
        this.car_data = this.getCartData();
        this.bindEvent();


        // 打印商品id
        this.data = null;
        if (!this.id) {
            return this.error();
        }
        let res = await this.loadData();
        this.data = this.id;
        this.olddata = res;
 
        this.render(this.id);

        new Magnifier({
            ele: ".goods-detail",
            pic: ".img-box",
            focus: ".focus",
            big: ".big-img",
            big_img: ".big-img img",
        });
    }

    loadData() {
        let options = {
            url: "../static/data/hufup.json",
            dataType: "json",
        }
        return $.ajax(options);

    }

    bindEvent() {
        on(this.buycar, "click", ".car", (e) => {
            console.log(2);
            let id = e.origin.getAttribute("data-id");
            console.log(id);
            let res = this.search(id);
            console.log(res);
            alert("添加成功");

            this.addCart(id, res);

        });
        
        $(".car").on("click", function () {
            location.href = "./shopping_cart.html";

            console.log(11 );
        })
    }
    search(id) {
        let newres = null;
        this.olddata.some(item => {
            // console.log(3);
            if (id === item.id) {
                newres = item;
                // console.log(newres);
                return true
            }
        })
        return newres;
    }
    getCartData() {
        try {
            let data = JSON.parse(localStorage.getItem("car"))
            console.log(data);
            if (data === null) {
                return {}
            }
            return data;
        } catch (e) {
            return {};
        }
    }
    addCart(id, data) {
        // 判断当前的id是否存在于购物车列表之中
        if (id in this.car_data) {
            // 如果存在就让商品数量自增; 
            console.log(this.car_data);
            this.car_data[id].count++;
        } else {
            // 如果不存在就放入一个初始数据; 
            data.count = 1;
            this.car_data[id] = olddata;
        }
        this.save();
    }
    save() {
        localStorage.setItem("car", JSON.stringify(this.car_data));
    }


    abort() {
        alert("数据错误，请重新选择商品")
        location.href = "./goodlist.html"
    }
    render(id) {
        let res = this.olddata;
        for (let i = 0; i < this.olddata.length; i++) {
            // console.log(i);
            let data_id = this.olddata[i].id;
            if (id === data_id) {
                this.img.src = res[i].src;
                this.big_img.src = res[i].src;
                this.min_img.src = res[i].src;
                this.title.innerHTML = res[i].title;
                this.price.innerHTML = res[i].price;
                var html = "";
                html += ` <a class="car" data-id="${id}">
                加入购物车
            </a>`
                $(".buycar").html(html);
            }
        }
    }


}
new GoodDetail;